<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="学员状态">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width: 240px">
          <el-option label="正常" value="normal" />
          <el-option label="禁用" value="disabled" />
        </el-select>
      </el-form-item>
      <el-form-item label="学员昵称">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入学员昵称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学员手机号">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入学员手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 学员列表 -->
    <el-table v-loading="loading" :data="studentList" style="width: 100%">
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="学员昵称" prop="nickname" />
      <el-table-column label="学员手机号" prop="phone" />
      <el-table-column label="性别" prop="gender" />
      <el-table-column label="购课/报名数量" prop="courseCount" />
      <el-table-column label="注册时间" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.status === 'normal' ? '正常' : '禁用' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleEnable(scope.row)">{{ scope.row.status === 'normal' ? '禁用' : '启用' }}</el-button>
          <el-button size="mini" type="text" icon="el-icon-key" @click="handleResetPassword(scope.row)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 编辑学员信息弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
      <el-form :model="studentInfo" label-width="80px">
        <el-form-item label="学员昵称">
          <el-input v-model="studentInfo.nickname" placeholder="请输入学员昵称" />
        </el-form-item>
        <el-form-item label="学员手机">
          <el-input v-model="studentInfo.phone" placeholder="请输入学员手机号" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="studentInfo.gender">
            <el-radio label="male">男</el-radio>
            <el-radio label="female">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveStudentInfo">保存</el-button>
      </span>
    </el-dialog>

    <!-- 确认禁用弹窗 -->
    <el-dialog
      title="确认禁用"
      :visible.sync="disableDialogVisible"
      width="30%"
    >
      <p>此操作将禁用<strong>{{ currentStudentNickname }}</strong>，是否继续?</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="disableDialogVisible = false">再想想</el-button>
        <el-button type="primary" @click="handleDisableStudent">确认禁用</el-button>
      </span>
    </el-dialog>

    <!-- 重置密码弹窗 -->
    <el-dialog
      title="重置密码"
      :visible.sync="resetPasswordDialogVisible"
      width="30%"
    >
      <p>请确认，是否重置<strong>{{ currentStudentNickname }}</strong>的密码?</p>
      <p>重置后，初始密码为<strong>123456</strong>，请通知学员尽快修改密码</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetPasswordDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleResetStudentPassword">重置</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { parseTime } from "@/utils/index";

export default {
  name: "StudentManagement",
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      studentList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: undefined,
        phone: undefined,
        status: undefined
      },
      dialogTitle: "编辑学员信息",
      dialogVisible: false,
      disableDialogVisible: false,
      resetPasswordDialogVisible: false,
      studentInfo: {
        nickname: '',
        phone: '',
        gender: 'male' // 默认性别为男
      },
      currentStudentNickname: '',
      // ... 其他数据
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      setTimeout(() => {
        this.loading = false;
        this.studentList = [
          { id: 1, nickname: "wi34du0p", phone: "12100000014", gender: "male", courseCount: 0, createTime: "2023-06-06T16:02:58", status: "normal" },
          { id: 2, nickname: "74bksh3c", phone: "12100000015", gender: "male", courseCount: 0, createTime: "2023-06-06T16:02:58", status: "normal" },
          // 更多模拟数据...
        ];
        this.total = this.studentList.length;
      }, 1000);
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
    },
    handleUpdate(row) {
      this.studentInfo = { ...row };
      this.dialogTitle = '编辑学员信息';
      this.dialogVisible = true;
    },
    handleEnable(row) {
      this.currentStudentNickname = row.nickname;
      this.disableDialogVisible = true;
    },
    handleResetPassword(row) {
      this.currentStudentNickname = row.nickname;
      this.resetPasswordDialogVisible = true;
    },
    handleDisableStudent() {
      // 执行禁用学员的逻辑
      console.log('禁用学员', this.currentStudentNickname);
      this.disableDialogVisible = false; // 关闭弹窗
    },
    handleResetStudentPassword() {
      // 执行重置学员密码的逻辑
      console.log('重置学员密码', this.currentStudentNickname);
      this.resetPasswordDialogVisible = false; // 关闭弹窗
    },
    saveStudentInfo() {
      // 保存学员信息的逻辑
      console.log('保存学员信息', this.studentInfo);
      this.dialogVisible = false; // 关闭弹窗
    },
    // ... 其他方法
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.dialog-footer {
  text-align: right;
}
</style>
